local M = {
  -- Formatting
  "stevearc/conform.nvim",
  enabled = true,
  event = { "BufReadPre", "BufNewFile" },
}

function M.config()
  local conform = require "conform"
  conform.setup {
    formatters_by_ft = {
      javascript = { "prettier" },
      typescript = { "prettier" },
      javascriptreact = { "prettier" },
      typescriptreact = { "prettier" },
      svelte = { "prettier" },
      css = { "prettier" },
      html = { "prettier" },
      json = { "prettier" },
      yaml = { "prettier" },
      markdown = { "prettier" },
      graphql = { "prettier" },
      lua = { "stylua" },
      -- python = { "isort", "black" },
      python = function(bufnr)
        if require("conform").get_formatter_info("ruff_format", bufnr).available then
          return { "ruff_format" }
        else
          return { "isort", "black" }
        end
      end,
    },
    format_on_save = nil,
    -- format_on_save = {
    --   -- 会自动就格式化了
    --   -- I recommend these options. See :help conform.format for details.
    --   timeout_ms = 500,
    --   lsp_format = "fallback",
    -- },
  }

  -- --  保存时格式化
  -- vim.api.nvim_create_autocmd({ "BufWritePre" }, {
  --   pattern = "*",
  --   callback = function(args)
  --     conform.format { bufnr = args.buf }
  --   end,
  -- })

  vim.keymap.set({ "n", "v" }, "<leader>fc", function()
    conform.format {
      lsp_fallback = true,
      async = false,
      timeout_ms = 500,
    }
  end, { desc = "Conform format file or range (in visual mode)" })
end

return M
